@echo off
setlocal enabledelayedexpansion

echo ==========================================
echo Sokit CLI Linux Cross Compile Script
echo ==========================================

echo.
echo This script will build Linux version of sokit-cli using cross compilation toolchain
echo.

REM Check required tools
echo Checking build tools...

REM Check CMake
where cmake >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: CMake not found
    echo Please install CMake: https://cmake.org/download/
    pause
    exit /b 1
)

REM Check MinGW or MSVC
where gcc >nul 2>&1
if %errorlevel% equ 0 (
    set COMPILER=gcc
    echo Found GCC compiler
) else (
    where cl >nul 2>&1
    if %errorlevel% equ 0 (
        set COMPILER=msvc
        echo Found MSVC compiler
    ) else (
        echo Error: No compiler found
        echo Please install MinGW or Visual Studio
        pause
        exit /b 1
    )
)

echo Tool check passed!
echo.

REM Select target architecture
echo Please select target Linux architecture:
echo 1) x86_64 (64-bit)
echo 2) x86 (32-bit)
echo 3) ARM64
echo 4) ARM32

set /p arch_choice="Enter your choice (1-4): "

if "%arch_choice%"=="1" (
    set TARGET_ARCH=x86_64-linux-gnu
    set CMAKE_SYSTEM_NAME=Linux
    set CMAKE_SYSTEM_PROCESSOR=x86_64
) else if "%arch_choice%"=="2" (
    set TARGET_ARCH=i686-linux-gnu
    set CMAKE_SYSTEM_NAME=Linux
    set CMAKE_SYSTEM_PROCESSOR=i686
) else if "%arch_choice%"=="3" (
    set TARGET_ARCH=aarch64-linux-gnu
    set CMAKE_SYSTEM_NAME=Linux
    set CMAKE_SYSTEM_PROCESSOR=aarch64
) else if "%arch_choice%"=="4" (
    set TARGET_ARCH=arm-linux-gnueabihf
    set CMAKE_SYSTEM_NAME=Linux
    set CMAKE_SYSTEM_PROCESSOR=arm
) else (
    echo 无效选择，使用默认x86_64
    set TARGET_ARCH=x86_64-linux-gnu
    set CMAKE_SYSTEM_NAME=Linux
    set CMAKE_SYSTEM_PROCESSOR=x86_64
)

echo 目标架构: %TARGET_ARCH%
echo.

REM 创建交叉编译工具链文件
echo 创建交叉编译工具链文件...

(
echo # 交叉编译工具链配置
echo set(CMAKE_SYSTEM_NAME %CMAKE_SYSTEM_NAME%^)
echo set(CMAKE_SYSTEM_PROCESSOR %CMAKE_SYSTEM_PROCESSOR%^)
echo.
echo # 指定C编译器
echo set(CMAKE_C_COMPILER %TARGET_ARCH%-gcc^)
echo.
echo # 指定C++编译器
echo set(CMAKE_CXX_COMPILER %TARGET_ARCH%-g++^)
echo.
echo # 指定查找路径
echo set(CMAKE_FIND_ROOT_PATH /usr/%TARGET_ARCH%^)
echo.
echo # 查找规则
echo set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER^)
echo set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY^)
echo set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY^)
echo.
echo # Qt6配置
echo set(QT_QMAKE_EXECUTABLE %TARGET_ARCH%-qmake-qt6^)
echo set(QT6_DIR /usr/%TARGET_ARCH%/lib/cmake/Qt6^)
) > toolchain-%TARGET_ARCH%.cmake

echo 工具链文件已创建: toolchain-%TARGET_ARCH%.cmake
echo.

REM 创建构建目录
echo 创建构建目录...
if not exist "build-cross" mkdir build-cross
cd build-cross

REM 配置CMake
echo 配置CMake...
cmake ..\src\sokit-cli ^
    -DCMAKE_TOOLCHAIN_FILE=..\toolchain-%TARGET_ARCH%.cmake ^
    -DCMAKE_BUILD_TYPE=Release ^
    -DCMAKE_INSTALL_PREFIX=/usr/local

if %errorlevel% neq 0 (
    echo.
    echo CMake配置失败!
    echo 可能的原因:
    echo 1. 未安装交叉编译工具链
    echo 2. Qt6交叉编译库未安装
    echo 3. 工具链配置错误
    echo.
    echo 建议使用Docker或WSL方法进行构建
    pause
    exit /b 1
)

REM 编译
echo 编译项目...
cmake --build . --config Release

if %errorlevel% neq 0 (
    echo.
    echo 编译失败!
    pause
    exit /b 1
)

REM 创建发布包
echo 创建发布包...
mkdir sokit-cli-linux-%TARGET_ARCH%
copy sokit-cli.exe sokit-cli-linux-%TARGET_ARCH%\
copy ..\README.md sokit-cli-linux-%TARGET_ARCH%\ 2>nul
copy ..\LINUX_BUILD_GUIDE.md sokit-cli-linux-%TARGET_ARCH%\ 2>nul

REM 创建zip包
echo 打包...
powershell -Command "Compress-Archive -Path sokit-cli-linux-%TARGET_ARCH% -DestinationPath sokit-cli-linux-%TARGET_ARCH%-%date:~0,4%%date:~5,2%%date:~8,2%.zip"

echo.
echo ==========================================
echo 交叉编译完成!
echo ==========================================
echo 可执行文件: build-cross\sokit-cli.exe
echo 发布包: build-cross\sokit-cli-linux-%TARGET_ARCH%-*.zip
echo ==========================================

REM 清理工具链文件
cd ..
del toolchain-%TARGET_ARCH%.cmake

pause
exit /b 0 